//
//  matchViewController.m
//  pe_score
//
//  Created by zwz on 16/6/26.
//  Copyright © 2016年 zhangwz. All rights reserved.
//

#import "matchViewController.h"
#import "matchTableViewCell.h"
#import "matchModel.h"
#import "addMatchViewController.h"

@interface matchViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_TableView;
    UIImageView *_imageView;
    
}
@property(nonatomic,copy)NSArray *mydataarray;

@end

@implementation matchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    
    [self creatTableView];
    [self creatView];
    [self getData];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)creatTableView
{
    _TableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-114)];
    _TableView.dataSource = self;
    _TableView.delegate = self;
    _imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@""]]];
    
    _TableView.backgroundColor = [UIColor clearColor];
    [_TableView setBackgroundView:_imageView];
    [self.view addSubview:_TableView];
}

-(void)creatView
{
    UIBarButtonItem * bar = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(clickNavBtn:)];
    bar.title = @"增加赛程";
    self.navigationItem.rightBarButtonItem=bar;
}


-(void)clickNavBtn:(UIBarButtonItem *)btn
{
    addMatchViewController *addMatch = [[addMatchViewController alloc] init];
    [self.navigationController pushViewController:addMatch animated:YES];
}

-(void)getData
{
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration ephemeralSessionConfiguration];
    config.timeoutIntervalForRequest = 30.0f;
    config.timeoutIntervalForResource = 60.0f;
    NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
    NSURLSessionDataTask *task = [session dataTaskWithURL:[NSURL URLWithString:@"http://23.105.222.161/abc/run_query.php?type=1"]completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSArray *res = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
        NSMutableArray *newMy = [[NSMutableArray alloc]init];
        for (NSDictionary *x in res) {
            matchModel *newModel = [matchModel new];
            newModel.hostTeam = [x valueForKey:@"host_name"];
            newModel.guestTeam = [x valueForKey:@"guest_name"];
            newModel.hostScore= [x valueForKey:@"live_host_score"];
            newModel.guestScore = [x valueForKey:@"live_guest_score"];
            newModel.dateYear = [x valueForKey:@"time_date_year"];
            newModel.dateMonth = [x valueForKey:@"time_date_month"];
            newModel.dateDay = [x valueForKey:@"time_date_day"];
            newModel.dateHour = [x valueForKey:@"time_hour"];
            newModel.dateMinute = [x valueForKey:@"time_minite"];
            newModel.gameState=[x valueForKey:@"match_state"];
            [newMy addObject:newModel];
        }
        _mydataarray = newMy;
        
        [_TableView reloadData];
    }];
    [task resume];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //return 10;
    return [_mydataarray count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 130;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellID = @"matchTableViewCell";
    matchTableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:cellID];
    cell.backgroundColor = nil;
    
    matchModel *model =_mydataarray[indexPath.row];
    
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:cellID owner:self options:nil] lastObject];
    }
    
    cell.hostTeam.text=model.hostTeam;
    cell.guestTeam.text=model.guestTeam;
    cell.hostScore.text=model.hostScore;
    cell.guestScore.text=model.guestScore;
    cell.dateYear.text=model.dateYear;
    cell.dateMonth.text=model.dateMonth;
    cell.dateDay.text=model.dateDay;
    cell.dateHour.text=model.dateHour;
    cell.dateMinute.text=model.dateMinute;
    cell.gameState.text=model.gameState;
    
    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}


@end
